package pathfind;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class NodeMap {
private int[][] node;
private int xsize,ysize;
private Point start;
public NodeMap(int xsize,int ysize){
	this.ysize=ysize;
	this.xsize=xsize;
	
	node = new int[xsize][ysize];
	
	for(int x = 0;x<xsize;x++){
		for(int y = 0;y<ysize;y++){
			node[x][y] = 0;
		}
	}
}
public void setblock(int x,int y,int block)
{
	node[x][y] = block;
}
public Point getstart()
{
	return start;
}
public boolean isblock(int x,int y){
	return node[x][y] == -1;
}
public int getNode(int x,int y){
	return node[x][y];
}
public int[][] getdisp()
{
	return node;
}
public int getsizeX()
{
	return xsize;
}
public int getsizeY()
{
	return ysize;
}
public void clean()
{
	for(int i = 0;i<xsize;i++)
	{
		for(int j = 0;j<ysize;j++)
		{
			if(node[i][j]>0)		node[i][j] = 0;
		}
	}
}
public void setMap(File f)
{
	try {
		Scanner in = new Scanner(f);
		for(int i = 0;i<ysize;i++)
		{
			char[] line = in.nextLine().toCharArray();
			for(int j = 0;j<xsize;j++)
			{
				if(line[j] == 'x') node[j][i] = -1;
				if(line[j] == 's') 
				{
					setStart(j,i);
				}
			}
		}
		in.close();
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	}
}
public void setStart(int x,int y)
{
	node[x][y] = 1;
	start = new Point(x,y);
}
public ArrayList<Point> spred(int x,int y)
{
	int value = node[x][y];
	ArrayList<Point> p = new ArrayList<Point>();
	if(x+1<xsize && y+1<ysize && node[x][y+1] != -1 && node[x+1][y] != -1 && node[x+1][y+1] == 0)
	{
		node[x+1][y+1] = value+1;
		p.add(new Point(x+1, y+1));
	}
	if(x-1>=0 && y-1>=0 && node[x][y-1] != -1 && node[x-1][y] != -1 && node[x-1][y-1] == 0)
	{
		node[x-1][y-1] = value+1;
		p.add(new Point(x-1, y-1));
	}
	if(x-1>=0 && y+1<ysize && node[x][y+1] != -1 && node[x-1][y] != -1 && node[x-1][y+1] == 0)
	{
		node[x-1][y+1] = value+1;
		p.add(new Point(x-1, y+1));
	}
	if(x+1<xsize && y-1>=0 && node[x][y-1] != -1 && node[x+1][y] != -1 && node[x+1][y-1] == 0)
	{
		node[x+1][y-1] = value+1;
		p.add(new Point(x+1, y-1));
	}
	
	if(x+1<xsize && node[x+1][y] == 0)
	{
		node[x+1][y] = value+1;
		p.add(new Point(x+1, y));
	}
	if(x-1>=0 && node[x-1][y] == 0)
	{
		node[x-1][y] = value+1;
		p.add(new Point(x-1, y));
	}
	if(y+1<ysize && node[x][y+1] == 0)
	{
		node[x][y+1] = value+1;
		p.add(new Point(x, y+1));
	}
	if(y-1>=0 && node[x][y-1] == 0)
	{
		node[x][y-1] = value+1;
		p.add(new Point(x, y-1));
	}
	
	return p;
}
}
